﻿using System;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using TravelAgency.DAL;

namespace TravelAgency.AspNet.Agents
{
    public partial class ManageTrips : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void RadListViewTrips_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.SelectCommandName)
            {
                RadListViewDataItem item = e.ListViewItem as RadListViewDataItem;
                string tripIdString = item.GetDataKeyValue("TripId").ToString();
                int tripId = int.Parse(tripIdString);

                TravelAgencyDAO dao = new TravelAgencyDAO();
                using (dao)
                {
                    Trip selectedTrip = dao.GetTripByTripId(tripId);
                    this.FillTripDate(selectedTrip);
                }
            }
        }

        private void FillTripDate(Trip selectedTrip)
        {
            if (selectedTrip != null)
            {
                this.PanelTrip.Visible = true;

                int tripCurrentCalacity = selectedTrip.CurrentPeopleCapacity;                
                this.LabelTripNotAvailable.Visible = false;
                this.PanelTripDetails.Visible = true;
                this.LabelTitle.Text = Server.HtmlEncode(selectedTrip.Title);
                this.LabelDescription.Text = Server.HtmlEncode(selectedTrip.Description);
                this.LabelStartDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.StartDate);
                this.LabelEndDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.EndDate);
                this.LabelPrice.Text = string.Format("{0:c}", selectedTrip.Price);
                this.LabelPeopleCount.Text = tripCurrentCalacity.ToString();
                this.LabelMaxPeopleCount.Text = selectedTrip.MaxPeopleCapacity.ToString();             
            }
        }

        protected string TitleWordWrapEval(string PropertyName)
        {
            string fullTitle = Eval(PropertyName).ToString();
            if (fullTitle.Length > 19)
            {
                string shortTitle = fullTitle.Substring(0, 16);
                return shortTitle + "...";
            }
            else
            {
                return fullTitle;
            }
        }

        protected void LinkButtonEdit_Click(object sender, EventArgs e)
        {
            string tripIdString =
                this.RadListViewTrips.SelectedItems[0].GetDataKeyValue("TripId").ToString();
            int tripId = int.Parse(tripIdString);
            this.Response.Redirect(
                string.Format("~/Agents/EditTrip.aspx?tripId={0}", tripId));
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            string tripIdString = this.RadListViewTrips.SelectedItems[0].GetDataKeyValue("TripId").ToString();
            int tripId = int.Parse(tripIdString);            
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                Trip selectedTrip = dao.GetTripByTripId(tripId);
                if (selectedTrip.Reservations.Count == 0)
                {                    
                    this.Response.Redirect("~/Agents/DeleteEmptyTrip.aspx?tripId=" + tripIdString);
                }
                else
                {
                    this.Response.Redirect("~/Agents/DeleteTripWithReservations.aspx?tripId=" + tripIdString);
                }
            }
        }

        protected void LinkButtonViewReservations_Click(object sender, EventArgs e)
        {
            string tripIdString =
                this.RadListViewTrips.SelectedItems[0].GetDataKeyValue("TripId").ToString();
            this.Response.Redirect("~/Agents/ViewReservationsOfTrip.aspx?tripId=" + tripIdString);
        }

        protected void LinkButtonSearch_Click(object sender, EventArgs e)
        {
            string searchPhrase = this.TextBoxSearch.Text;
            this.RadListViewTrips.FilterExpressions.Clear();
            this.RadListViewTrips.FilterExpressions.BuildExpression().
                Contains("Title", searchPhrase).Build();
            this.RadListViewTrips.Rebind();

            this.PanelTrip.Visible = false;
        }        
    }
}